package com.dd.admin.common.exception;

import com.dd.admin.common.model.result.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理API异常
     */
    @ExceptionHandler(ApiException.class)
    public ResultBean<Object> handleApiException(ApiException ex) {
        log.error("API异常：{}", ex.getMessage(), ex);
        return ResultBean.error(ex.getCode(), ex.getMessage());
    }

    /**
     * 处理文件上传大小超限异常
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResultBean<Object> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex) {
        log.error("文件上传大小超限：{}", ex.getMessage());
        return ResultBean.error("文件大小超出限制，请上传小于100MB的文件");
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public ResultBean<Object> handleException(Exception ex) {
        log.error("系统异常：{}", ex.getMessage(), ex);
        return ResultBean.error("系统异常，请联系管理员");
    }
}